package gui;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.*;

import logica.Mapa;

public class Ventana implements WindowListener {
	private final Dimension PANTALLA = Toolkit.getDefaultToolkit().getScreenSize();
	private static int DEFAULT_FPS = 80; // FPS por Defecto
	
	private JFrame frame;
	private JPanel lienzo;
	private Mapa map;
	
	private Lienzo motor;

	public Ventana() {
		this.initFPS();
		this.initialize();
		frame.setVisible(true);
		
		motor.startGame();
//		map.init();
	}

	/**
	 * Se inicializa la Interfaz
	 */
	private void initialize() {
		frame = new JFrame();
//		frame.setResizable(false);
		frame.setSize(1233, 700);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
//		map = new Mapa();
		lienzo = motor;
		lienzo.setSize(frame.getSize());
		lienzo.setLocation(0, 0);
		frame.add(lienzo);
		
		frame.addComponentListener(new ComponentAdapter () {
			@Override
			public void componentResized(ComponentEvent e) {
				lienzo.setSize(frame.getSize());
			}
		});
	}
	
	/**
	 * Se Inicia el panel y se le envia el periodo de repeticion
	 */
	private void initFPS () {
		int fps = DEFAULT_FPS;
		long period = (long) 1000.0/fps; // tiempo de repeticion
		System.out.println("Fps: "+fps+"; Period: "+period+" ms");
		
		motor = new Lienzo(this, period*1000000L); // ms -> ns
	}
	

	/**
	 * Eventos de la Ventana
	 */
	
	@Override
	public void windowActivated(WindowEvent e) {
		motor.resumeGame();
	}
	
	@Override
	public void windowDeactivated(WindowEvent e) {
		motor.pauseGame();
	}

	@Override
	public void windowIconified(WindowEvent e) {
		motor.pauseGame();
	}
	
	@Override
	public void windowDeiconified(WindowEvent e) {
		motor.resumeGame();
	}
	
	@Override
	public void windowOpened(WindowEvent e) {}

	@Override
	public void windowClosing(WindowEvent e) {}
	
	@Override
	public void windowClosed(WindowEvent e) {}
}